#include "stdafx.h"
#include "GDlgMgr.h"
#include "GFactory.h"
#include "GMainDlg.h"
#include "GLoginDlg.h"
#include "GTeamDlg.h"

GDlgMgr g_DlgMgr;

GDlgMgr::GDlgMgr(void)
{
	GFactory<GDialog>::Instance().Reg(_T("GMainDlg"),&Create<GDialog, GMainDlg>);
	GFactory<GDialog>::Instance().Reg(_T("GLoginDlg"),&Create<GDialog, GLoginDlg>);
	GFactory<GDialog>::Instance().Reg(_T("GTeamDlg"),&Create<GDialog, GTeamDlg>);

	GDialog::RegisterWndClass();
}
GDlgMgr& GDlgMgr::Inst()
{
	return g_DlgMgr;
}
GDlgMgr::~GDlgMgr(void)
{
	map<wstring, GDialog*>::iterator iter;
	for (iter = m_mapDlg.begin(); iter!= m_mapDlg.end(); ++iter)
	{
		delete iter->second;
	}
	m_mapDlg.clear();
}

GDialog* GDlgMgr::GetDlg(const wchar_t* strDlg)
{
	map<wstring, GDialog*>::iterator iter;
	iter = m_mapDlg.find(strDlg);
	if (iter == m_mapDlg.end())
	{
		assert(0);
		return NULL;
	}
	return iter->second;
}

void GDlgMgr::Init()
{
	map<wstring, GDialog*>::iterator iter;
	GLoginDlg* pDlg = (GLoginDlg*)GetDlg(_T("GLoginDlg"));
	pDlg->SetActivated(TRUE);
}
void GDlgMgr::LoadDialog(const char* szPath)
{
	TiXmlDocument doc;  
	if (!doc.LoadFile(szPath))
	{  	
		assert(0);
		return;
	}
	TiXmlElement* DlgMgrElem = doc.RootElement();
	for (TiXmlElement* DlgElem = DlgMgrElem->FirstChildElement(); 
		DlgElem!=NULL; DlgElem = DlgElem->NextSiblingElement())
	{
		map<tstring, tstring> mapAtt;	
		for(const TiXmlAttribute *pAtt  = DlgElem->FirstAttribute(); pAtt!=NULL; pAtt = pAtt->Next())
		{
			pair<map<tstring, tstring>::iterator, bool> ret;
			ret = mapAtt.insert(make_pair(conversion::UTF8ToUnicode(pAtt->Name()),
				conversion::UTF8ToUnicode(pAtt->Value())));
			if (ret.second == false) assert(0);
		}

		map<tstring, tstring>::iterator iter;
		iter = mapAtt.find(_T("type"));
		if (iter == mapAtt.end()) assert(0);
		GDialog* pDlg = GFactory<GDialog>::Instance().CreateProduct(iter->second);
		pDlg->LoadResAttritute(mapAtt);	
		pDlg->LoadResource(DlgElem);
		pDlg->SetSubWnd();
		AddDialog(iter->second.c_str(), pDlg);
	}
}
void GDlgMgr::AddDialog(const wchar_t* str, GDialog* pDlg)
{
	m_mapDlg.insert(make_pair(str, pDlg));
}
void GDlgMgr::SetViewPort(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, w, 0, h);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void GDlgMgr::DrawDialog(int w, int h)
{
	map<wstring, GDialog*>::reverse_iterator iter;
	for (iter = m_mapDlg.rbegin(); iter != m_mapDlg.rend(); ++iter)
	{
		if (iter->second->IsActivated())
		{
			(iter->second)->Draw();
		}		
	}
}
void GDlgMgr::SendMsg(UINT uMsg, int wParem, int lParam)
{
	map<wstring, GDialog*>::iterator iter;
	GDialog* pDlg  = NULL;
	for (iter = m_mapDlg.begin(); iter!= m_mapDlg.end(); ++iter)
	{
		(iter->second)->SendMsg(uMsg, wParem, lParam);
	}
}
void GDlgMgr::MouseMove(int x, int y,int wParam)
{
	map<wstring, GDialog*>::iterator iter;
	GDialog* pDlg  = NULL;
	for (iter = m_mapDlg.begin(); iter!= m_mapDlg.end(); ++iter)
	{
		pDlg = iter->second;
		if (pDlg->IsActivated() && pDlg->IsEnableMove())
		{
			pDlg->MouseMove(x, y, wParam);
			break;
		}
	}
}
void GDlgMgr::MouseClick(int button, int state, int x, int y)
{
	map<wstring, GDialog*>::iterator iter;
	GDialog* pDlg  = NULL;
	for (iter = m_mapDlg.begin(); iter!= m_mapDlg.end(); ++iter)
	{
		pDlg = iter->second;
		if (pDlg->IsPointInDialog(x, y) && pDlg->IsActivated())
		{
			pDlg->MouseClick(button, state, x, y);
			break;
		}
	}
}
